package com.jingtai.platform.service.shop.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingtai.common.enums.DeleteFlagEnum;
import com.jingtai.common.enums.EnableStatusEnum;
import com.jingtai.common.template.ResultMapInfo;
import com.jingtai.core.entity.shop.GoodsSpecificationsValue;
import com.jingtai.core.mapper.shop.GoodsSpecificationsValueMapper;
import com.jingtai.platform.service.shop.GoodsSpecificationsValueService;
import com.jingtai.platform.util.ResultUtil;
import com.jingtai.platform.util.UrlToMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * 商品规格属性值表 impl
 *
 * @author sunlaiqian
 * @date 2020-12-09 15:39:05
 */
@Service
public class GoodsSpecificationsValueServiceImpl extends ServiceImpl<GoodsSpecificationsValueMapper, GoodsSpecificationsValue> implements GoodsSpecificationsValueService {

    @Autowired
    private GoodsSpecificationsValueService goodsSpecificationsValueService;

    @Override
    public ResultUtil GoodsSpecificationsValuelist(Integer page, Integer limit, String conditions){
        QueryWrapper<GoodsSpecificationsValue> wrapper = new QueryWrapper<>();
        Map<String, String> map = UrlToMap.paramToMap(conditions);
        for(Map.Entry<String, String> entry : map.entrySet()){
            wrapper.like(entry.getKey(), entry.getValue());
        }
        wrapper.eq("delete_flag", DeleteFlagEnum.NOT_DELETE.getCode());
        return new ResultUtil(ResultMapInfo.GETSUCCESS,goodsSpecificationsValueService.page(new Page<>(page,limit),wrapper));
    }

    @Override
    public ResultUtil addGoodsSpecificationsValue(List<GoodsSpecificationsValue> goodsSpecificationsValue) {
        boolean save = goodsSpecificationsValueService.saveBatch(goodsSpecificationsValue);
        return new ResultUtil(save ? ResultMapInfo.ADDSUCCESS:ResultMapInfo.ADDFAIL);
    }

    @Override
    public ResultUtil getGoodsSpecificationsValueById(Long id) {
        GoodsSpecificationsValue goodsSpecificationsValue = goodsSpecificationsValueService.getById(id);
        return goodsSpecificationsValue == null ?  new ResultUtil(ResultMapInfo.GETFAIL) : new ResultUtil(ResultMapInfo.GETSUCCESS,goodsSpecificationsValue);
    }

    @Override
    public ResultUtil delGoodsSpecificationsValueById(Long id) {
        GoodsSpecificationsValue goodsSpecificationsValue = new GoodsSpecificationsValue();
        goodsSpecificationsValue.setId(id);
        goodsSpecificationsValue.setDeleteFlag(DeleteFlagEnum.DELETE.getCode());
        boolean delState = goodsSpecificationsValueService.updateById(goodsSpecificationsValue);
        return delState ?  new ResultUtil(ResultMapInfo.DELETESUCCESS) : new ResultUtil(ResultMapInfo.DELETEFAIL);
    }

    @Override
    public ResultUtil selectGoodsSpecificationsValueBySpecificationsId(Long id) {
        List<GoodsSpecificationsValue> list = goodsSpecificationsValueService.list(Wrappers.lambdaQuery(GoodsSpecificationsValue.class).eq(GoodsSpecificationsValue::getSpecificationsId, id).eq(GoodsSpecificationsValue::getEnableState, EnableStatusEnum.NORMAL.getValue()).eq(GoodsSpecificationsValue::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
        return new ResultUtil(ResultMapInfo.GETSUCCESS,list);
    }

    @Override
    public ResultUtil editGoodsSpecificationsValue(List<GoodsSpecificationsValue> goodsSpecificationsValue) {
        goodsSpecificationsValueService.remove(Wrappers.lambdaQuery(GoodsSpecificationsValue.class).eq(GoodsSpecificationsValue::getSpecificationsId,goodsSpecificationsValue.get(0).getSpecificationsId()));
        boolean editState = goodsSpecificationsValueService.saveBatch(goodsSpecificationsValue);
        return editState ?  new ResultUtil(ResultMapInfo.EDITSUCCESS) : new ResultUtil(ResultMapInfo.EDITFAIL);
    }

}
